package cscie160.hw6;
/**
 * This class implements a minimal wrapper needed to hold the state information
 * that models a bank account.
 * Methods are synchronized for the sake of using class in multithreaded application.
 * @author Michael Melnikov   
*/
public class Account
{
    /**
    * balance on the account
    */
    private Float balance;

    /**
    * no-args constructor creates account with zero balance
    */   
    public Account()
    {
        balance = new Float(0.0);
    }
    
    /**
    * deposits sum equal to its argument.
    * throws ATMException at the attempt to deposit a negative amount
    */
    public void deposit(float amount) throws ATMException
    {
        synchronized (this){
        System.out.println("\t"+ getClass().getName() + " method=deposit  balance=" + balance);
        if (amount < 0)
            throw (new ATMException("negative amount can't be deposited."));
        balance += amount;
        System.out.println("\t"+ getClass().getName() + " after deposit  balance=" + balance);
        }
    }
    
    /**
    * withdraws sum equal to its argument 
    * throws ATMException at the attempt to withdraw a negative amount or amount exceeding the balance 
    */    
    public void withdraw(float amount) throws ATMException
    {
        synchronized (this){
        System.out.println("\t"+ getClass().getName() + " method=withdraw  balance=" + balance);
        if (amount > balance)
            throw (new ATMException("insufficient funds."));

        if (amount < 0)
            throw (new ATMException("negative amount can't be withdrawn."));
        balance -= amount; 
        System.out.println("\t"+ getClass().getName() + " after withdraw  balance=" + balance);
        }  
    }    
    
    /**
    * returns balance on the account
    */
    protected Float getBalance()
    {
        synchronized (this){
        System.out.println("\t"+ getClass().getName() + " method=getBalance  balance=" + balance);
        return balance;  
        }  
    }
}

